/*
 * 文件名：SamplingTableServiceImpl.java
 * 版权：Copyright by www.tiansu-china.com/
 * 描述：
 * 修改人：congriran
 * 修改时间：2020年12月3日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
*/

package com.tiansu.reborn.wanda.modules.executor.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.tiansu.reborn.wanda.modules.executor.dao.SamplingTableDao;
import com.tiansu.reborn.wanda.modules.executor.entity.vo.CreateSamplingTableVO;
import com.tiansu.reborn.wanda.modules.executor.service.SamplingTableService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SamplingTableServiceImpl implements SamplingTableService
{

    @Autowired
    private SamplingTableDao dao;

    @Override
    @Transactional
    public int repairSamplingTable(CreateSamplingTableVO vo)
    {
        String tablePrefix = vo.getTablePrefix();
        Integer startNo = vo.getStartNo();
        Integer endNo = vo.getEndNo();
        int result = 0;
        while (startNo <= endNo)
        {
            String tableName = tablePrefix + startNo;
            String name = dao.existTable(tableName);
            if (StringUtils.isEmpty(name))
            {
                dao.createSamplingTable(tableName);
                dao.createIndex(tableName);
                result++;
            }
            else
            {
                log.info("Table: " + tableName + " already exists!");
            }
            startNo++;
        }

        return result;
    }

}
